package com.test.mylotterysystem.controller;

import com.test.mylotterysystem.comment.utils.MyBeanUtil;
import com.test.mylotterysystem.controller.Param.PasswordLoginParam;
import com.test.mylotterysystem.controller.Param.UserRegisterParam;
import com.test.mylotterysystem.controller.Param.VerificationCodeLoginParam;
import com.test.mylotterysystem.controller.Result.UserLoginResult;
import com.test.mylotterysystem.controller.Result.UserRegisterResult;
import com.test.mylotterysystem.service.DTO.UserLoginDTO;
import com.test.mylotterysystem.service.DTO.UserRegisterDTO;
import com.test.mylotterysystem.service.UserService;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     *
     * @param userRegisterParam
     * @return
     */
    @RequestMapping("/register")
    public UserRegisterResult register(@Validated @RequestBody UserRegisterParam userRegisterParam) {
        log.info("userRegisterParam为：,{}", userRegisterParam);
        UserRegisterDTO userRegisterDTO = userService.register(userRegisterParam);
        return MyBeanUtil.transOfUserRegisterResult(userRegisterDTO);
    }

    /**
     * 发送短信验证码
     *
     * @param phoneNumber
     * @return
     */
    @RequestMapping("/verificationCode/send")
    public boolean verificationCode(@NotBlank(message = "手机号为空") String phoneNumber) {
        log.info("手机号为：" + phoneNumber);
        return userService.verificationCode(phoneNumber);
    }

    /**
     * 使用密码登录
     *
     * @param passwordLoginParam
     * @return
     */
    @RequestMapping("/passwordLogin")
    public UserLoginResult passwordLogin(@Validated @RequestBody PasswordLoginParam passwordLoginParam) {
        log.info("passwordLoginParam为：{}", passwordLoginParam);
        UserLoginDTO userLoginDTO = userService.login(passwordLoginParam);
        return MyBeanUtil.transOfUserLoginResult(userLoginDTO);
    }

    /**
     * 使用验证码登录
     *
     * @param verificationCodeLoginParam
     * @return
     */
    @RequestMapping("/verificationCodeLogin")
    public UserLoginResult verificationCodeLogin(@Validated @RequestBody VerificationCodeLoginParam verificationCodeLoginParam) {
        log.info("verificationCodeLoginParam为：{}", verificationCodeLoginParam);
        UserLoginDTO userLoginDTO = userService.login(verificationCodeLoginParam);
        return MyBeanUtil.transOfUserLoginResult(userLoginDTO);
    }
}
